package br.com.admin.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@SuppressWarnings({"rawtypes"})
public class BaseDAO {

	@PersistenceContext
	protected EntityManager entityManager;

    public List findByQuery(String queryString) {
        final Query query = this.entityManager.createQuery(queryString.toString());
        return query.getResultList();
    }

	public Object persist(Object entity) {
        this.entityManager.persist(entity);
        return entity;
    }

    public void remove(Class<?> entityClass, Object id) {
        this.entityManager.remove(this.entityManager.getReference(entityClass, id));
    }

    public Object find(Class<?> entityClass, Object id) {
        return (Object) this.entityManager.find(entityClass, id);
    }

    public Object merge(Object entity) {
        return this.entityManager.merge(entity);    
    }
}
